/*
 * seg_led.h
 *
 * Segment LED driver.
 *
 * Copyright (C) 2019 Sinh Micro, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: lipeng<lp@sinhmicro.com>
 */

#ifndef __SEG_LED_H__
#define __SEG_LED_H__

#include "hal-config.h"
#include "hal-regs.h"
#include "hal/inc/types.h"
#include "hal/inc/gpio.h"
#include "hal/inc/timer.h"
#include "hal/inc/system.h"
#include "battery.h"
#include "ec_app_config.h"
#include "oil.h"
#include "ui_config.h"

#define SEG_NUM                     (5)
#define USER_VALUE_NUM              (3)

#define SEG_BAT                     (1 << 0)
#define SEG_ELI                     (1 << 1)
#define SEG_PCT                     (1 << 2)

#define SEG_ON_ALL                  (1 << 14)
#define SEG_OFF_ALL                 (1 << 15)

#define BIT0                        (1 << 0)
#define BIT1                        (1 << 1)
#define BIT2                        (1 << 2)
#define BIT3                        (1 << 3)
#define BIT4                        (1 << 4)
#define BIT5                        (1 << 5)
#define BIT6                        (1 << 6)
#define BIT7                        (1 << 7)

#define SEG_1_PWM               P04_PWM
#define SEG_1_ON                P04_ON
#define SEG_1_OFF               P04_OFF
#define SEG_1_IN                P04_IN
#define SEG_1_INIT              P04_INIT

#define SEG_2_PWM               P05_PWM
#define SEG_2_ON                P05_ON
#define SEG_2_OFF               P05_OFF
#define SEG_2_IN                P05_IN
#define SEG_2_INIT              P05_INIT

#define SEG_3_PWM               P06_PWM
#define SEG_3_ON                P06_ON
#define SEG_3_OFF               P06_OFF
#define SEG_3_IN                P06_IN
#define SEG_3_INIT              P06_INIT

#define SEG_4_PWM               P10_PWM
#define SEG_4_ON                P10_ON
#define SEG_4_OFF               P10_OFF
#define SEG_4_IN                P10_IN
#define SEG_4_INIT              P10_INIT

#define SEG_5_PWM               P14_PWM
#define SEG_5_ON                P14_ON
#define SEG_5_OFF               P14_OFF
#define SEG_5_IN                P14_IN
#define SEG_5_INIT              P14_INIT

#define SEG_6_PWM               P17_PWM
#define SEG_6_ON                P17_ON
#define SEG_6_OFF               P17_OFF
#define SEG_6_IN                P17_IN
#define SEG_6_INIT              P17_INIT

#define SEG_7_PWM               P15_PWM
#define SEG_7_ON                P15_ON
#define SEG_7_OFF               P15_OFF
#define SEG_7_IN                P15_IN
#define SEG_7_INIT              P15_INIT

#define SEG_8_PWM               P16_PWM
#define SEG_8_ON                P16_ON
#define SEG_8_OFF               P16_OFF
#define SEG_8_IN                P16_IN
#define SEG_8_INIT              P16_INIT

#define SEG_9_PWM               P22_PWM
#define SEG_9_ON                P22_ON
#define SEG_9_OFF               P22_OFF
#define SEG_9_IN                P22_IN
#define SEG_9_INIT              P22_INIT

#define SEG_10_PWM               P23_PWM
#define SEG_10_ON                P23_ON
#define SEG_10_OFF               P23_OFF
#define SEG_10_IN                P23_IN
#define SEG_10_INIT              P23_INIT

#define SEG_START()     (T0EN = 1)
#define SEG_EXIT()      (T0EN = 0)

#if (CONFIG_USING_SEG_LED == 1)

void seg_display(uint8_t value, uint16_t type);
void seg_init(void);
void seg_exit(void);
#else
#define seg_display(x,y)
#define seg_init()
#define seg_exit()
#endif

#endif

#define P00_PWM                 P0OM |= (1 << 0);               MFP0 = (MFP0 & ~0x03) | 0x01;   MFP3 = (MFP3 & ~0x60)
#define P00_ON                  P0ODAT |=  (1 << 0);            P0OE  |=  (1 << 0);
#define P00_OFF                 P0ODAT &= ~(1 << 0);            P0OE  |=  (1 << 0);
#define P00_IN                  MFP0 = (MFP0 & ~0x03);          MFP3 = (MFP3 & ~0x60)           P0OE  &= ~(1 << 0);     P0OM &= ~(1 << 0);
#define P00_INIT                MFP0 = (MFP0 & ~0x03);          MFP3 = (MFP3 & ~0x60)           P0DRV |=  (1 << 0);

#define P01_PWM                 P0OM |= (1 << 1);               MFP0 = (MFP0 & ~0x0C) | 0x04;
#define P01_ON                  P0ODAT |= (1 << 1);             P0OE |= (1 << 1);
#define P01_OFF                 P0ODAT &= ~(1 << 1);            P0OE |= (1 << 1);
#define P01_IN                  MFP0 = (MFP0 & ~0x0C);          P0OE &= ~(1 << 1);  P0OM &= ~(1 << 1);
#define P01_INIT                MFP0 = (MFP0 & ~0x0C);          P0DRV |= (1 << 1);

#define P02_PWM                 P0OM |= (1 << 2);               MFP0 = (MFP0 & ~0x30) | 0x10;
#define P02_ON                  P0ODAT |= (1 << 2);             P0OE |= (1 << 2);
#define P02_OFF                 P0ODAT &= ~(1 << 2);            P0OE |= (1 << 2);
#define P02_IN                  MFP0 = (MFP0 & ~0x30);          P0OE &= ~(1 << 2);  P0OM &= ~(1 << 2);
#define P02_INIT                MFP0 = (MFP0 & ~0x30);          P0DRV |= (1 << 2);

#define P03_PWM                 P0OM |= (1 << 3);               MFP0 = (MFP0 & ~0xC0) | 0x40;
#define P03_ON                  P0ODAT |= (1 << 3);             P0OE |= (1 << 3);
#define P03_OFF                 P0ODAT &= ~(1 << 3);            P0OE |= (1 << 3);
#define P03_IN                  MFP0 = (MFP0 & ~0xC0);          P0OE &= ~(1 << 3);  P0OM &= ~(1 << 3);
#define P03_INIT                MFP0 = (MFP0 & ~0xC0);          P0DRV |= (1 << 3);

#define P04_PWM                 P0OM |= (1 << 4);               MFP1 = (MFP1 & ~0x03) | 0x01;
#define P04_ON                  P0ODAT |= (1 << 4);             P0OE |= (1 << 4);
#define P04_OFF                 P0ODAT &= ~(1 << 4);            P0OE |= (1 << 4);
#define P04_IN                  MFP1 = (MFP1 & ~0x03);          P0OE &= ~(1 << 4);  P0OM &= ~(1 << 4);
#define P04_INIT                MFP1 = (MFP1 & ~0x03);          P0DRV |= (1 << 4);

#define P05_PWM                 P0OM |= (1 << 5);               MFP1 = (MFP1 & ~0x0C) | 0x04;
#define P05_ON                  P0ODAT |= (1 << 5);             P0OE |= (1 << 5);
#define P05_OFF                 P0ODAT &= ~(1 << 5);            P0OE |= (1 << 5);
#define P05_IN                  MFP1 = (MFP1 & ~0x0C);          P0OE &= ~(1 << 5);  P0OM &= ~(1 << 5);
#define P05_INIT                MFP1 = (MFP1 & ~0x0C);          P0DRV |= (1 << 5);

#define P06_PWM                 P0OM |= (1 << 6);               MFP1 = (MFP1 & ~0x30) | 0x10;   
#define P06_ON                  P0ODAT |= (1 << 6);             P0OE |= (1 << 6);
#define P06_OFF                 P0ODAT &= ~(1 << 6);            P0OE |= (1 << 6);
#define P06_IN                  MFP1 = (MFP1 & ~0x30);          P0OE &= ~(1 << 6);  P0OM &= ~(1 << 6);
#define P06_INIT                MFP1 = (MFP1 & ~0x30);          P0DRV |= (1 << 6);

#define P07_PWM                 P0OM |= (1 << 7);               MFP1 = (MFP1 & ~0xC0) | 0x40;   
#define P07_ON                  P0ODAT |= (1 << 7);             P0OE |= (1 << 7);
#define P07_OFF                 P0ODAT &= ~(1 << 7);            P0OE |= (1 << 7);
#define P07_IN                  MFP1 = (MFP1 & ~0xC0);          P0OE &= ~(1 << 7);  P0OM &= ~(1 << 7);
#define P07_INIT                MFP1 = (MFP1 & ~0xC0);          P0DRV |= (1 << 7);

#define P10_PWM                 P1OM |= (1 << 0);               MFP2 = (MFP2 & ~0x83) | 0x80;   
#define P10_ON                  P1ODAT |= (1 << 0);             P1OE |= (1 << 0);
#define P10_OFF                 P1ODAT &= ~(1 << 0);            P1OE |= (1 << 0);
#define P10_IN                  MFP2 = (MFP2 & ~0x83);          P1OE &= ~(1 << 0); P1OM &= ~(1 << 0);
#define P10_INIT                MFP2 = (MFP2 & ~0x83);          P1DRV = (P1DRV & ~0x03) | 0x01;

#define P11_PWM                 P1OM |= (1 << 1);               MFP2 = (MFP2 & ~0x0C) | 0x08;
#define P11_ON                  P1ODAT |= (1 << 1);             P1OE |= (1 << 1);
#define P11_OFF                 P1ODAT &= ~(1 << 1);            P1OE |= (1 << 1);
#define P11_IN                  MFP2 = (MFP2 & ~0x0C);          P1OE &= ~(1 << 1); P1OM &= ~(1 << 1);
#define P11_INIT                MFP2 = (MFP2 & ~0x0C);          P1DRV = (P1DRV & ~0x0C) | 0x04;

#define P12_PWM                 P1OM |= (1 << 2);               MFP2 = (MFP2 & ~0x30) | 0x20;   
#define P12_ON                  P1ODAT |= (1 << 2);             P1OE |= (1 << 2);
#define P12_OFF                 P1ODAT &= ~(1 << 2);            P1OE |= (1 << 2);
#define P12_IN                  MFP2 = (MFP2 & ~0x30);          P1OE &= ~(1 << 2); P1OM &= ~(1 << 2);
#define P12_INIT                MFP2 = (MFP2 & ~0x30);          P1DRV = (P1DRV & ~0x30) | 0x10;

#define P13_PWM                 P1OM |= (1 << 3);               MFP2 = (MFP2 & ~0x40) | 0x40;   
#define P13_ON                  P1ODAT |= (1 << 3);             P1OE |= (1 << 3);
#define P13_OFF                 P1ODAT &= ~(1 << 3);            P1OE |= (1 << 3);
#define P13_IN                  MFP2 = (MFP2 & ~0x40);          P1OE &= ~(1 << 3); P1OM &= ~(1 << 3);
#define P13_INIT                MFP2 = (MFP2 & ~0x40);          P1DRV = (P1DRV & ~0xC0) | 0x40;

#define P14_PWM                 P1OM |= (1 << 4);               MFP3 = (MFP3 & ~0x03) | 0x01;              
#define P14_ON                  P1ODAT |= (1 << 4);             P1OE |= (1 << 4);
#define P14_OFF                 P1ODAT &= ~(1 << 4);            P1OE |= (1 << 4);
#define P14_IN                  MFP3 = (MFP3 & ~0x03);          P1OE &= ~(1 << 4);
#define P14_INIT                MFP3 = (MFP3 & ~0x03);          MFP3 = (MFP3 & ~0x0C) | 0x04;